//
//  ZCSponsorVC.m
//  zctx
//
//  Created by apple on 15/12/30.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCSponsorVC.h"
#import "ZCProjectdetailsSponsorCell.h"
@interface ZCSponsorVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,assign)NSInteger offset;
@end

@implementation ZCSponsorVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCSponsorVC"];
    [MTA trackPageViewBegin:@"ZCSponsorVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCSponsorVC"];
    [MTA trackPageViewEnd:@"ZCSponsorVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initnavegation];
    self.navigationItem.title = @"支持者";
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [[NSMutableArray alloc]init];
    _offset = 0;
    
     NSLog(@"1");
    //个人信息请求
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self requestData:YES];
    });
    
    [self initTableView]; //初始化UITableView
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCProjectdetailsSponsorCell" bundle:nil] forCellReuseIdentifier:@"sponsorcell"];
    self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark 加载更多
-(void)loadMoreData{
    //上拉加载
    _offset = _offset+1;
    [self requestData:NO];
}


-(void)requestData:(BOOL)animation{
     NSLog(@"2");
    NSLog(@"pid = %@",self.strPid);
    if (animation) {
        [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    }

    [NetworkManager requestFoundingGetOneSponsorWithId:self.strPid Mid:UserModel.mid password:UserModel.password pagecount:[NSString stringWithFormat:@"%ld",(long)_offset] success:^(NSDictionary *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        [self.tableView.mj_footer endRefreshing];
        NSObject *dict =(NSObject *)[dictResponse objectForKey:@"content"];
        if ([dict isKindOfClass:[NSArray class]]){
            NSArray *array = (NSArray *)dict;
            if (array.count>0) {
                for (NSDictionary *dict in array) {
                    ZCInvolvedDetailsModelHotSponsors *model = [ZCInvolvedDetailsModelHotSponsors modelObjectWithDictionary:dict];
                    [self.dataArray addObject:model];
                }
                [self.tableView reloadData];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        self.bannerView.hidden = NO;
    }];
}


#pragma mark 导航栏
-(void)initnavegation{
    
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initTableView{
     NSLog(@"3");
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0, 0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *IDcell = @"sponsorcell";
    ZCProjectdetailsSponsorCell *cell = [tableView dequeueReusableCellWithIdentifier:IDcell];
    if (cell == nil) {
        cell = [[ZCProjectdetailsSponsorCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDcell];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.Model =self.dataArray[indexPath.row];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.000001;
    }else{
        return 10;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"4");
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

}

//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 10)];
//    return headView;
//}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}


@end
